package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcCheckRecord;
import com.csun.cmny.provider.model.domain.OmcInStockRecord;
import com.csun.cmny.provider.model.domain.OmcInventory;
import com.csun.cmny.provider.model.domain.OmcLifeAmbiguity;
import com.csun.cmny.provider.model.dto.inventory.InStockDto;
import com.csun.cmny.provider.model.dto.lifeAmbiguity.LifeAmbiguitySaveDto;
import com.csun.cmny.provider.model.dto.lifeAmbiguity.LifeAmbiguitySelectDto;
import com.csun.cmny.provider.model.vo.lifeAmbiguity.LifeAmbiguityVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcLifeAmbiguityService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/lifeAmbiguity", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcLifeAmbiguityController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcLifeAmbiguityService ambiguityService;

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "设置模糊度")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addOmcContract(@RequestBody @Valid LifeAmbiguitySaveDto lifeAmbiguitySaveDto, BindingResult br){

        OmcLifeAmbiguity omcLifeAmbiguity = new OmcLifeAmbiguity();
        omcLifeAmbiguity.setGroupId(getLoginAuthDto().getGroupId());

        List<OmcLifeAmbiguity> list = ambiguityService.select(omcLifeAmbiguity);
        if (PublicUtil.isNotEmpty(list)){
            omcLifeAmbiguity.setId(list.get(0).getId());
            omcLifeAmbiguity.setAmbiguity(lifeAmbiguitySaveDto.getAmbiguity());
            int update = ambiguityService.update(omcLifeAmbiguity);
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
        }

        omcLifeAmbiguity.setAmbiguity(lifeAmbiguitySaveDto.getAmbiguity());
        int result = ambiguityService.save(omcLifeAmbiguity);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ApiOperation(httpMethod = "POST", value = "模糊度查询")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select(@RequestBody LifeAmbiguitySelectDto lifeAmbiguitySelectDto){

        lifeAmbiguitySelectDto.setGroupId(getLoginAuthDto().getGroupId());
        List<LifeAmbiguityVo>list=ambiguityService.selectAmbiguity(lifeAmbiguitySelectDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),list);
    }
}
